package tech.aistar.service.impl;


import org.apache.ibatis.session.SqlSession;
import tech.aistar.entity.Phone;
import tech.aistar.mapper.PhoneMapper;
import tech.aistar.service.IPhoneService;
import tech.aistar.util.MybatisUtil;

import java.util.List;

public class PhoneServiceImpl implements IPhoneService {

    //模糊查找
    @Override
    public List<Phone> findAll(String name, Integer type_id) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        PhoneMapper mapper = sqlSession.getMapper(PhoneMapper.class);

        List<Phone> list = mapper.findAll(name,type_id);

        MybatisUtil.close(sqlSession);
        return list;
    }

    @Override
    public void delById(Integer id) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        PhoneMapper mapper = sqlSession.getMapper(PhoneMapper.class);

        mapper.delById(id);

        sqlSession.commit();

        MybatisUtil.close(sqlSession);
    }

    @Override
    public void add(Phone phone) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        PhoneMapper mapper = sqlSession.getMapper(PhoneMapper.class);

        mapper.add(phone);

        sqlSession.commit();

        MybatisUtil.close(sqlSession);
    }

    @Override
    public void updateById(Phone phone) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        PhoneMapper mapper = sqlSession.getMapper(PhoneMapper.class);

        mapper.updateById(phone);

        sqlSession.commit();

        MybatisUtil.close(sqlSession);
    }

    @Override
    public Phone findById(Integer id) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        PhoneMapper mapper = sqlSession.getMapper(PhoneMapper.class);

        Phone phone = mapper.findById(id);

        MybatisUtil.close(sqlSession);

        return phone;
    }
}
